var Field = function(opts) {
	opts = opts || {
		type : 'error'
	};
	/**
	 * 只读控件
	 */
	this.createDisplayField = function(opts) {
		return new Ext.form.DisplayField(opts);
	};
	/**
	 * 创建文本框
	 */
	this.createText = function(opts) {
		// 返回只读控件
		if (opts.readOnly === true) {
			return this.createDisplayField(opts);
		}
		if (!opts.spell || !opts.isQuery) {
			return new Ext.form.TextField(Ext.apply({}, opts));
			/*
			 * return new Ext.form.CompositeField({ fieldLabel :
			 * opts.fieldLabel, hidden : opts.hidden, disabled : opts.disabled,
			 * items : [Ext.apply({ xtype : 'textfield', flex : 1 }, opts), {
			 * xtype : 'combo', editable : false, twinTrigger : false,
			 * triggerAction : 'all', modal : 'local', name : '@match', store :
			 * [['=', '精确'], ['like', '模糊'], ['like_left', '左模糊'],
			 * ['like_right', '右模糊']], value : 'like', width : 60, listeners : {
			 * select : function(t, rec) { var p = t.previousSibling(); var name =
			 * p.getName(); var area = name.indexOf('\|') > -1 ? name
			 * .split('\|')[0] : 'a'; var field = name.indexOf('#') > -1 ?
			 * name.split('#')[1] : name.indexOf('\|') > -1 ? name
			 * .split('\|')[1] : name; name = area + '|' + rec.data.field1 + '#' +
			 * field; p.el.dom.name = name; } } }] });
			 */
		}
		opts.name = opts.name + "@" + opts.spell;
		return new Ext.form.CompositeField({
					fieldLabel : opts.fieldLabel,
					hidden : opts.hidden,
					disabled : opts.disabled,
					items : [Ext.apply({
										xtype : 'textfield',
										flex : 1
									}, opts), {
								xtype : 'checkbox',
								boxLabel : '同音',
								name : '@spell',
								inputValue : opts.spell,
								checked : true,
								listeners : {
									check : function(t, checked) {
										var p = t.previousSibling();
										if (checked) {
											p.el.dom.name = p.getName()
													.split('@')[0]
													+ "@" + opts.spell;
										} else {
											p.el.dom.name = p.getName()
													.split('@')[0];
										}
									}
								}
							}, {
								xtype : 'combo',
								editable : false,
								twinTrigger : false,
								triggerAction : 'all',
								modal : 'local',
								name : '@match',
								store : [['=', '精确'], ['like', '模糊'],
										['like_left', '左模糊'],
										['like_right', '右模糊']],
								value : 'like',
								width : 60,
								listeners : {
									select : function(t, rec) {
										var p = t.previousSibling()
												.previousSibling();
										var name = p.getName();
										var area = name.indexOf('\|') > -1
												? name.split('\|')[0]
												: 'a';
										var field = name.indexOf('#') > -1
												? name.split('#')[1]
												: name.indexOf('\|') > -1
														? name.split('\|')[1]
														: name;
										name = area + '|' + rec.data.field1
												+ '#' + field;
										p.el.dom.name = name;
									}
								}
							}]
				});
	};
	/**
	 * 创建隐藏框
	 */
	this.createHidden = function(opts) {
		return new Ext.form.Hidden(opts);
	};
	/**
	 * 创建文本域
	 */
	this.createTextArea = function(opts) {
		// 返回只读控件
		if (opts.readOnly === true) {
			return this.createDisplayField(opts);
		}
		return new Ext.form.TextArea(Ext.apply({}, opts));
	};
	/**
	 * 创建数字框
	 */
	this.createNumber = function(opts) {
		var number;
		if (!opts.range) {
			// 返回只读控件
			if (opts.readOnly === true) {
				return this.createDisplayField(opts);
			}
			number = new Ext.form.NumberField(Ext.apply({}, opts));
		} else {
			var start = new Ext.form.NumberField(Ext.applyIf({
						flex : 1,
						name : opts.name + '@start',
						value : opts.startValue || opts.value,
						listeners : {
							focus : function() {
								if (end.isValid()) {
									this.setMaxValue(end.getValue());
								}
							},
							blur : function() {
								if (this.isValid()) {
									end.setMinValue(this.getValue());
									end.validate();
								}
							}
						}
					}, opts));
			var end = new Ext.form.NumberField(Ext.applyIf({
						flex : 1,
						name : opts.name + '@end',
						value : opts.endValue || opts.value,
						listeners : {
							focus : function() {
								if (start.isValid()) {
									this.setMinValue(start.getValue());
								}
							},
							blur : function() {
								if (this.isValid()) {
									start.setMaxValue(this.getValue());
									start.validate();
								}
							}
						}
					}, opts));
			number = new Ext.form.CompositeField({
						fieldLabel : opts.fieldLabel,
						items : [start, {
									xtype : 'label',
									width : 8,
									text : ' - '
								}, end]
					});
		}
		return number;
	};
	/**
	 * 创建日期控件
	 */
	this.createDate = function(opts) {
		var date;
		if (!opts.range) {
			// 返回只读控件
			if (opts.readOnly === true) {
				if (Ext.isDate(opts.value)) {
					opts.value = opts.value.format(opts.format || 'Y-m-d');
				}
				return this.createDisplayField(opts);
			}
			date = new Ext.form.DateField(Ext.apply({}, opts));
		} else {
			var start = new Ext.form.DateField(Ext.applyIf({
						xtype : 'datefield',
						flex : 1,
						name : opts.name + '@start',
						value : opts.startValue || opts.value,
						listeners : {
							focus : function() {
								var v = end.getValue() || new Date();
								this.setMaxValue(v);
							},
							blur : function() {
								if (this.isValid()) {
									end.setMinValue(this.getValue());
									end.validate();
								}
							}
						}
					}, opts));
			var end = new Ext.form.DateField(Ext.applyIf({
						xtype : 'datefield',
						flex : 1,
						name : opts.name + '@end',
						value : opts.endValue || opts.value,
						maxValue : new Date(),
						listeners : {
							focus : function() {
								this.setMinValue(start.getValue());
							},
							blur : function() {
								if (this.isValid()) {
									start.setMaxValue(this.getValue());
									start.validate();
								}
							}
						}
					}, opts));
			date = new Ext.form.CompositeField({
						fieldLabel : opts.fieldLabel,
						items : [start, {
									xtype : 'label',
									width : 8,
									text : ' - '
								}, end]
					});
		}
		return date;
	};
	/**
	 * 创建日期时间控件
	 */
	this.createDateTime = function(opts) {
		var dateTime;
		if (!opts.range) {
			// 返回只读控件
			if (opts.readOnly === true) {
				if (Ext.isDate(opts.value)) {
					opts.value = opts.value
							.format(opts.format || 'Y-m-d H:i:s');
				}
				return this.createDisplayField(opts);
			}
			dateTime = new Ext.ux.form.DateTimeField(Ext.apply({}, opts));
		} else {
			var start = new Ext.ux.form.DateTimeField(Ext.applyIf({
						xtype : 'datetimefield',
						flex : 1,
						name : opts.name + '@start',
						value : opts.startValue || opts.value,
						listeners : {
							focus : function() {
								var v = end.getValue()
										|| (new Date().format('Y-m-d') + " 23:59:59");
								this.setMaxValue(v);
							},
							blur : function() {
								if (this.isValid()) {
									end.setMinValue(this.getValue());
									end.validate();
								}
							}
						}
					}, opts));
			var end = new Ext.ux.form.DateTimeField(Ext.applyIf({
						xtype : 'datetimefield',
						flex : 1,
						name : opts.name + '@end',
						value : opts.endValue || opts.value,
						maxValue : (new Date().format('Y-m-d') + " 23:59:59"),
						listeners : {
							focus : function() {
								this.setMinValue(start.getValue());
							},
							blur : function() {
								if (this.isValid()) {
									start.setMaxValue(this.getValue());
									start.validate();
								}
							}
						}
					}, opts));
			dateTime = new Ext.form.CompositeField({
						fieldLabel : opts.fieldLabel,
						items : [start, {
									xtype : 'label',
									width : 8,
									text : ' - '
								}, end]
					});
		}
		return dateTime;
	};
	/**
	 * 创建文件上传控件
	 */
	this.createFile = function(opts) {
		var file;
		if (opts.maxSize && opts.maxSize > 0) {
			file = new Ext.form.CompositeField({
				fieldLabel : opts.fieldLabel,
				items : [{
							xtype : 'hidden',
							name : '@maxSize',
							value : opts.maxSize
						}, Ext.apply({
							xtype : 'fileuploadfield',
							buttonText : '',
							buttonCfg : {
								iconCls : 'icon-upload'
							},
							listeners : {
								fileselected : function(t, v) {
									if (opts.fileFormat) {
										var s = v
												.substr(v.lastIndexOf('.') + 1);
										var format = opts.fileFormat
												.toLowerCase();
										if (format.indexOf(s.toLowerCase()) == -1) {
											My.alert('请选择格式为 ' + format
													+ " 的文件");
											t.reset();
										}
									}
								}
							},
							flex : 1
						}, opts)]
			});
		} else {
			file = new Ext.form.FileUploadField(Ext.apply({
						buttonText : '',
						buttonCfg : {
							iconCls : 'icon-upload'
						},
						listeners : {
							fileselected : function(t, v) {
								if (opts.fileFormat) {
									var s = v.substr(v.lastIndexOf('.') + 1);
									var format = opts.fileFormat.toLowerCase();
									if (format.indexOf(s.toLowerCase()) == -1) {
										My.alert('请选择格式为 ' + format + " 的文件");
										t.reset();
									}
								}
							}
						}
					}, opts));
		}
		return file;
	};
	/**
	 * 创建下拉框
	 */
	this.createCombo = function(opts) {
		opts.emptyText = opts.emptyText || '--请选择--';
		var store;
		if (!opts.data) {
			var url = My.url('/code/getCodeList.xhtml');
			if (!opts.params) {
				url = My.url('/code/getCodeListByCache.xhtml');
			}
			store = new Ext.data.JsonStore({
						fields : [opts.valueField, opts.displayField],
						url : url,
						baseParams : {
							table : opts.code,
							where : opts.params
						},
						autoLoad : true
					});
		} else {
			store = new Ext.data.SimpleStore({
						fields : [opts.valueField, opts.displayField],
						data : opts.data
					});
		}
		// 删除不相干的属性
		delete opts.code;
		delete opts.data;
		delete opts.fields;
		// 隐藏字段名
		opts.hiddenName = opts.hiddenName || opts.name;
		if (!opts.multi) {
			// 返回只读控件
			if (opts.readOnly === true) {
				var display = this.createDisplayField(opts);
				store.on('load', function(t) {
					var i = store.find(opts.valueField, opts.value);
					if (i > -1) {
						display.setValue(store.getAt(i).get(opts.displayField));
					}
				});
				return display;
			}
			var combo = new Ext.form.ComboBox(Ext.apply({
						triggerAction : 'all',
						mode : 'local',
						store : store
					}, opts));
			return combo;
		} else {
			// 返回只读控件
			if (opts.readOnly === true) {
				var display = this.createDisplayField(opts);
				store.on('load', function() {
							var v = opts.value || '';
							var t = '';
							var values = v.split(',');
							Ext.each(values, function(value) {
										if (t.length > 0) {
											t += ',';
										}
										var i = store.find(opts.valueField,
												value);
										if (i > -1) {
											t += store.getAt(i)
													.get(opts.displayField);
										}
									});
							display.setValue(t);
						});
				return display;
			}
			return new LovCombo(Ext.apply({
						triggerAction : 'all',
						mode : 'local',
						store : store
					}, opts));
		}
	};
	/**
	 * 创建复选框
	 */
	this.createCheckbox = function(opts) {
		return new Ext.form.Checkbox(Ext.apply({
					inputValue : 1,
					checked : opts.checked || opts.value == 1
				}, opts));
	};
	/**
	 * 创建复选框组
	 */
	this.createCheckboxGroup = function(opts) {
		var items = [], v = opts.value ? String(opts.value).split(',') : [];
		Ext.each(opts.data, function(data) {
					items.push({
								boxLabel : data[1],
								name : opts.name,
								inputValue : data[0],
								checked : v.indexOf(String(data[0])) != -1
							});
				});
		opts.columns = opts.columns && opts.columns > 0 ? opts.columns : 'auto';
		return new Ext.form.CheckboxGroup(Ext.apply({
					columns : opts.columns,
					items : items
				}, opts));
	};
	/**
	 * 创建单选按钮
	 */
	this.createRadio = function(opts) {
		return new Ext.form.Radio(Ext.apply({
					inputValue : 1,
					checked : opts.checked || opts.value == 1
				}, opts));
	};

	/**
	 * 创建单选按钮组
	 */
	this.createRadioGroup = function(opts) {
		var items = [];
		Ext.each(opts.data, function(data) {
					items.push({
								boxLabel : data[1],
								name : opts.name,
								inputValue : data[0],
								checked : opts.value == data[0]
							});
				});
		opts.columns = opts.columns && opts.columns > 0 ? opts.columns : 'auto';
		return new Ext.form.RadioGroup(Ext.apply({
					columns : opts.columns,
					items : items
				}, opts));
	};
	/**
	 * 创建字典控件
	 */
	this.createCode = function(opts) {
		opts.emptyText = opts.emptyText || '--请选择--';
		opts.multiSelect = opts.multi;
		opts.table = opts.code;
		opts.queryWhere = opts.params;
		opts.spellField = opts.spell;
		return new Code(opts);
	};
	/**
	 * 下拉树
	 */
	this.createComboTree = function(opts) {
		opts.emptyText = opts.emptyText || '--请选择--';
		opts.multiSelect = opts.multi;
		opts.table = opts.code;
		opts.rootFilter = opts.params;
		opts.spellField = opts.spell;
		return new Ext.ux.SuperComboTree(opts);
	};

	// 生成各种字段
	var field;
	switch (opts.type) {
		case 'text' :// 文本框
			delete opts.type;
			field = this.createText(opts);
			break;
		case 'hidden' :// 隐藏框
			delete opts.type;
			field = this.createHidden(opts);
			break;
		case 'textarea' :// 文本域
			delete opts.type;
			field = this.createTextArea(opts);
			break;
		case 'number' :// 数字控件
			delete opts.type;
			field = this.createNumber(opts);
			break;
		case 'date' :// 日期控件
			delete opts.type;
			field = this.createDate(opts);
			break;
		case 'datetime' :// 日期时间控件
			delete opts.type;
			field = this.createDateTime(opts);
			break;
		case 'file' :// 文件上传控件
			delete opts.type;
			field = this.createFile(opts);
			break;
		case 'combo' :// 下拉框
			delete opts.type;
			field = this.createCombo(opts);
			break;
		case 'checkbox' :// 复选框
			delete opts.type;
			field = this.createCheckbox(opts);
			break;
		case 'checkboxgroup' :// 复选框组
			delete opts.type;
			field = this.createCheckboxGroup(opts);
			break;
		case 'radio' :// 单选按钮
			delete opts.type;
			field = this.createRadio(opts);
			break;
		case 'radiogroup' :// 单选按钮组
			delete opts.type;
			field = this.createRadioGroup(opts);
			break;
		case 'code' :// 字典控件
			delete opts.type;
			field = this.createCode(opts);
			break;
		case 'combotree' :// 下拉树
			delete opts.type;
			field = this.createComboTree(opts);
			break;
		default :
			field = new Ext.form.DisplayField({
						fieldLabel : '配置错误'
					});
			break;
	}
	return field;
};